﻿/********************************************************************
	created:	2011/05/24
	created:	24:5:2011   15:03
	filename: 	CommonFunctionClass
	file path:	G:\Users\neng\CodePlex\WiFiMTS\IntranetMessagerClient\Client\CommonFunctionClass.cs
	file base:	ServiceRepository
	file ext:	cs
	author:		邢远扬、黄能、邱文军
	
	purpose:	接口定义，基础功能类的内部成员声明，以及其构造函数
*********************************************************************/
using CommonLibrary;

namespace Client
{
    internal static class CommonFunctionClass
    {
        internal static void SendData(byte[] messageSend)
        {
            MainWindow.MainWindowChannel.CreateChannel().SendData(messageSend);
        }

        internal static byte[] SealMessageToEnvelope<T>(T msgContent, int uid,
                                                        bool isDesEncrypt, bool isRsaEncrypt)
        {
            var envelope = new Protocol.MessageEnvelope
                               {
                                   MessageType = msgContent.GetType(),
                                   MyUid = uid,
                                   ByteMessage = Functions.SerializeToBytes(msgContent)
                               };

            if (uid != -1)
            {
                if (isDesEncrypt)
                {
                    envelope.ByteMessage =
                        CryptoClient.DesEncrypt(MainWindow.MainWindowThisUser.DesKey,
                                                MainWindow.MainWindowThisUser.DesIv, envelope.ByteMessage);
                }
            }

            envelope.HashCode = Functions.MessageHash(envelope.ByteMessage);

            if (!isRsaEncrypt)
                envelope.Signed = false;
            else
            {
                envelope.ByteMessage = CryptoClient.RsaEncrypt(envelope.ByteMessage);
                
                //first communcate need RSA Encrupt
                envelope.Signed = true;
            }

            return Functions.SerializeToBytes(envelope);
        }
    }
}